package com.ruoyi.material.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.domain.BaseFlieRecord;
import com.ruoyi.material.domain.BaseLabel;
import com.ruoyi.material.domain.BaseMaterial;
import com.ruoyi.material.domain.vo.FileRecordVo;
import com.ruoyi.material.domain.vo.FileTotalInfoVo;
import com.ruoyi.material.mapper.BaseFlieRecordMapper;
import com.ruoyi.material.mapper.BaseLabelMapper;
import com.ruoyi.material.mapper.BaseMaterialMapper;
import com.ruoyi.material.service.IBaseFlieRecordService;
import com.ruoyi.material.service.IBaseLabelService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-10
 */
@Service
public class BaseFlieRecordServiceImpl implements IBaseFlieRecordService 
{
    @Autowired(required=false)
    private BaseFlieRecordMapper baseFlieRecordMapper;
    @Autowired(required=false)
    private BaseMaterialMapper baseMaterialMapper;
    @Autowired
    private IBaseLabelService baseLabelService;
    /**
     * 查询文件记录
     * 
     * @param id 文件记录ID
     * @return 文件记录
     */
    @Override
    public BaseFlieRecord selectBaseFlieRecordById(Long id)
    {
        return baseFlieRecordMapper.selectBaseFlieRecordById(id);
    }

    /**
     * 查询文件记录列表
     * 
     * @param baseFlieRecord 文件记录
     * @return 文件记录
     */
    @Override
    public List<BaseFlieRecord> selectBaseFlieRecordList(BaseFlieRecord baseFlieRecord)
    {
        return baseFlieRecordMapper.selectBaseFlieRecordList(baseFlieRecord);
    }

    /**
     * 新增文件记录
     * 
     * @param baseFlieRecord 文件记录
     * @return 结果
     */
    @Override
    public int insertBaseFlieRecord(BaseFlieRecord baseFlieRecord)
    {
        baseFlieRecord.setCreateTime(DateUtils.getNowDate());
        return baseFlieRecordMapper.insertBaseFlieRecord(baseFlieRecord);
    }

    /**
     * 修改文件记录
     * 
     * @param baseFlieRecord 文件记录
     * @return 结果
     */
    @Override
    public int updateBaseFlieRecord(BaseFlieRecord baseFlieRecord)
    {
        baseFlieRecord.setUpdateTime(DateUtils.getNowDate());
        return baseFlieRecordMapper.updateBaseFlieRecord(baseFlieRecord);
    }

    /**
     * 批量删除文件记录
     * 
     * @param ids 需要删除的文件记录ID
     * @return 结果
     */
    @Override
    public int deleteBaseFlieRecordByIds(Long[] ids)
    {
        return baseFlieRecordMapper.deleteBaseFlieRecordByIds(ids);
    }

    /**
     * 删除文件记录信息
     * 
     * @param id 文件记录ID
     * @return 结果
     */
    @Override
    public int deleteBaseFlieRecordById(Long id)
    {
        return baseFlieRecordMapper.deleteBaseFlieRecordById(id);
    }

    /**
     * 批量添加
     * @param list
     * @return
     */
    @Override
    public int addBaseFlieRecordList(List<BaseFlieRecord> list)
    {
        return baseFlieRecordMapper.addBaseFlieRecordList(list);
    }

    @Override
    public List<BaseFlieRecord> selectBaseFlieRecordByPcode(BaseFlieRecord baseFlieRecord, String pcode) {
        List<BaseMaterial> list = baseMaterialMapper.selectBaseFlieRecordByPcode(pcode);

        List<Long> ids = new ArrayList<Long>();
        for(int i = 0; i < list.size(); i++){
            Long id = Long.valueOf(list.get(i).getFileCode());
            ids.add(id);

        }

        List<BaseFlieRecord> data = baseFlieRecordMapper.selectBaseFlieRecordByIdsAndBaseFileRecord(baseFlieRecord,ids);
        return data;
    }

    @Override
    public List<FileRecordVo> selectBaseFlieRecordvOByPCodeAndBaseFileRecord(BaseFlieRecord baseFlieRecord, String pcode) {
        List<FileRecordVo> res = baseFlieRecordMapper.selectBaseFlieRecordvOByPCodeAndBaseFileRecord(baseFlieRecord,pcode);
        // 通过ID获取标签名称的方法
        for (FileRecordVo record: res){
            List<String> labels = baseLabelService.selectBaseLabelByFileCode(""+record.getId());
            record.setLabels(labels.toArray(new String[labels.size()]));
        }
        return res;
    }

    public FileTotalInfoVo selectTotalInfo(){
        return  baseFlieRecordMapper.selectTotalInfo();
    }


    /**
     * 查询文件记录，根据path_file_name
     *
     * @param path_file_name
     * @return 文件记录
     */
    @Override
    public BaseFlieRecord selectBaseFlieRecordByPathFileName(String path_file_name){
        return baseFlieRecordMapper.selectBaseFlieRecordByPathFileName(path_file_name);
    }


    /**
     * 根据已经找的的id、filecode的map进行批量删除
     *
     * @param list 上一级的code,本级的p_code
     * @param updateBy
     * @return 结果
     */
    @Override
    public int delBaseFlieRecordList(@Param("list") List<Integer> list, @Param("updateBy") String updateBy){
        return baseFlieRecordMapper.delBaseFlieRecordList(list,updateBy);
    }

}
